package com.ruoyi.openapiadmin.service.impl;

import java.util.HashMap;
import java.util.List;

import com.qf.qfsy.constants.CacheConstants;
import com.qf.qfsy.mqbean.ApimappingMqBean;
import com.qf.qfsy.utils.ObjectToHashCoverter;
import com.ruoyi.openapiadmin.feign.CacheFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.openapiadmin.mapper.ApiMappingMapper;
import com.ruoyi.openapiadmin.domain.ApiMapping;
import com.ruoyi.openapiadmin.service.IApiMappingService;
import com.ruoyi.common.core.text.Convert;

/**
 * 服务路由信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-11
 */
@Service
@Slf4j
public class ApiMappingServiceImpl implements IApiMappingService 
{
    @Autowired
    private ApiMappingMapper apiMappingMapper;

    @Autowired
    private CacheFeignClient cacheFeignClient;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 查询服务路由信息
     * 
     * @param id 服务路由信息主键
     * @return 服务路由信息
     */
    @Override
    public ApiMapping selectApiMappingById(Long id)
    {
        return apiMappingMapper.selectApiMappingById(id);
    }

    /**
     * 查询服务路由信息列表
     * 
     * @param apiMapping 服务路由信息
     * @return 服务路由信息
     */
    @Override
    public List<ApiMapping> selectApiMappingList(ApiMapping apiMapping)
    {
        return apiMappingMapper.selectApiMappingList(apiMapping);
    }

    /**
     * 新增服务路由信息
     * 
     * @param apiMapping 服务路由信息
     * @return 结果
     */
    @Override
    public int insertApiMapping(ApiMapping apiMapping)
    {
        int i = apiMappingMapper.insertApiMapping(apiMapping);

        // 把新增的路由信息写入缓存中
        String key = CacheConstants.OPENAPI_MAPPING_PREFIX +apiMapping.getGatewayApiName();
        cacheFeignClient.hmset(key,apiMapping);

        log.info("新增路由成功，并且写入了缓存模块中，key={},value={}",key,apiMapping);

        // 当新增路由后，封装一个消息写入mq
        String exchage = "openapi-apimapping-exchange";
        String routingkey = "openapi.apimapping."+apiMapping.getId();

        ApimappingMqBean apimappingMqBean = new ApimappingMqBean();
        apimappingMqBean.setType("add"); // 新增

        // java对象 ----> map
        HashMap<String, Object> hashMap = ObjectToHashCoverter.convertToHashMap(apiMapping);
        log.info("把路由信息对象={}转换成hashmap={}",apiMapping,hashMap);

        apimappingMqBean.setData(hashMap);
        apimappingMqBean.setKey(key);

        rabbitTemplate.convertAndSend(exchage,routingkey,apimappingMqBean);

        return i;
    }

    /**
     * 修改服务路由信息
     * 
     * @param apiMapping 服务路由信息
     * @return 结果
     */
    @Override
    public int updateApiMapping(ApiMapping apiMapping)
    {
        int i = apiMappingMapper.updateApiMapping(apiMapping);
        String key = CacheConstants.OPENAPI_MAPPING_PREFIX +apiMapping.getGatewayApiName();
        // 把修改的路由信息写入缓存中
        if (apiMapping.getState().equals(0)){
            cacheFeignClient.hmset(key,apiMapping);

            log.info("新增路由成功，并且写入了缓存模块中，key={},value={}",key,apiMapping);
        }else {
            // 把缓存中的该条路由信息删除调
        }

        // 当新增路由后，封装一个消息写入mq
        String exchage = "openapi-apimapping-exchange";
        String routingkey = "openapi.apimapping."+apiMapping.getId();

        ApimappingMqBean apimappingMqBean = new ApimappingMqBean();
        apimappingMqBean.setType("update"); // 新增

        // java对象 ----> map
        HashMap<String, Object> hashMap = ObjectToHashCoverter.convertToHashMap(apiMapping);
        log.info("把路由信息对象={}转换成hashmap={}",apiMapping,hashMap);

        apimappingMqBean.setData(hashMap);
        apimappingMqBean.setKey(key);

        rabbitTemplate.convertAndSend(exchage,routingkey,apimappingMqBean);


        return i;
    }

    /**
     * 批量删除服务路由信息
     * 
     * @param ids 需要删除的服务路由信息主键
     * @return 结果
     */
    @Override
    public int deleteApiMappingByIds(String ids)
    {
        return apiMappingMapper.deleteApiMappingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务路由信息信息
     * 
     * @param id 服务路由信息主键
     * @return 结果
     */
    @Override
    public int deleteApiMappingById(Long id)
    {
        return apiMappingMapper.deleteApiMappingById(id);
    }
}
